package newx.com.loader
{
	import flash.events.*;
	import flash.net.*;
	import flash.utils.ByteArray;
	
	import newx.BaseObject;
	
    public class StreamDataLoader extends BaseObject
	{
		private static	var byteData	:BaseObject	=	new BaseObject();
		
		private 		var loader		:URLLoader;
		
		private 		var _complete	:Function;
		private 		var _target		:Object;
		
        public function StreamDataLoader(type:String)
        {
        	loader				=	new URLLoader();
        	loader.dataFormat	=	URLLoaderDataFormat.BINARY;
        }
        
        public function load(request:URLRequest,complete:Function,target:Object):void
        {
        	_complete	=	complete;
        	_target		=	target;
        	configureListeners(loader);
        	loader.load(request);
        }
        
        /**
         *  
         * @param name	：数据容器名字
         * @param arr	：{key:String,url:String}
         * 
         */        
        public static function loadList(name:String,type:String,arr:Array,tObj:BaseObject):void
        {
        	tObj.HandleProperties("LoadList",arr);
        	tObj.HandleProperties("cLoading",0);
        	
        	byteData.HandleProperties(name,tObj)
        	
        	var tArr	:Array	=	new Array();
        	
        	var tempArr	:Array	=	arr;
        	
        	var tSize	:int	=	tempArr.length;
        	
        	var tKey	:String	=	"";
        	var tUrl	:String	=	"";
        	
        	for(var i:int = 0 ; i < tSize ; i++)
        	{
        		tKey	=	tempArr[i].key;
        		tUrl	=	tempArr[i].url;
        		if(tKey == "" || tUrl == "")
        		{
        			throw new Error("key or url is null!");
        			return;
        		}
        		for(var j:int = i+1 ; j < tSize ; j++)
	        	{
	        		if(tKey	== tempArr[j].key || tUrl == tempArr[j].url)
	        		{
	        			throw new Error("key or url can not be override!");
	        			return;
	        		}
	        	}
        	}
        	
        	var tLoader		:StreamDataLoader;
        	var onLoadOver	:Function	=	function(val1:StreamDataLoader,data:ByteArray):void
        	{
        		var tLoadObj:BaseObject	=	byteData.GetProperties(val1.GetProperties("contenName"));
        		
	            var tSize	:uint		=	(tLoadObj.GetProperties("LoadList") as Array).length
	            var cLoading:uint		=	tLoadObj.GetProperties("cLoading");
	            
	            cLoading++;
	            
	            tLoadObj.HandleProperties(val1.GetProperties("key"),data);
	            tLoadObj.HandleProperties("cLoading",cLoading);
	            
	            tLoadObj.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS,
	            	false,false,cLoading,tSize));
	            
	            if(cLoading>=tSize)
	            {
	            	tLoadObj.dispatchEvent(new Event(Event.COMPLETE));
	            }
        	}
        	
        	for(i = 0 ; i < tSize ; i++)
        	{
        		tKey				=	tempArr[i].key;
        		tUrl				=	tempArr[i].url;        		      		
        		tLoader				=	new StreamDataLoader(type);
        		
        		tLoader.HandleProperties("key",tKey)
        		tLoader.HandleProperties("contenName",name)
        		
        		tLoader.load(new URLRequest(tUrl),onLoadOver,StreamDataLoader);
        	}        	
        }
        
        public static function getByteArrayData(name:String,key:String):ByteArray
        {
        	 var tObj	:BaseObject	=	byteData.GetProperties(name);
        	 return tObj.GetProperties(key);
        }
        
        public static function getStringData(name:String,key:String):String
        {
        	 var tObj	:BaseObject	=	byteData.GetProperties(name);
        	 return tObj.GetProperties(key);
        }
        
        private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
//            dispatcher.addEventListener(Event.OPEN, openHandler);
//            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
//            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
//            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
//            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }

        private function completeHandler(event:Event):void
        {
        	_complete.call(_target,this,loader.data);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("ioErrorHandler: " + event);
        }
    }

}